package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import domain.Contact;
import domain.DAOContact;

/**
 * Servlet implementation class ModifyContact
 */
public class ModifyContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object isSearch=request.getSession().getAttribute("search");
		if(isSearch!=null){
			String idString=request.getParameter("id");
			if(idString!=null){
					request.getSession().removeAttribute("search");
					request.setAttribute("id", idString);
					long id=Long.parseLong(idString);
					DAOContact dao=new DAOContact();
					Contact c=dao.getContact(id);
					request.setAttribute("contact",c);
					getServletContext().getRequestDispatcher("/modifyContactForm.jsp").forward(request, response);
			}else{
					getServletContext().getRequestDispatcher("/modifyContact.jsp").forward(request, response);
			}
		}else{
			String id=request.getParameter("id");
			long idContact=Long.parseLong(id);
			String firstName=request.getParameter("firstname");
			String lastName=request.getParameter("lastname");
			String email=request.getParameter("email");
			DAOContact dao=DAOContact.getInstance();
			dao.modifyContact(idContact, firstName, lastName, email);
		}
	}

}
